package extension

import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.TypedValue
import android.widget.TextView
import androidx.annotation.Px
import androidx.core.widget.TextViewCompat
import java.lang.Exception


// TextView使用autosize时，高度设置为Wrap_content时，文字更新后大小发生变更，需要重新设置TextView大写。而由于autosize，则直接setTextSize无效，需要先关闭，再修改，再打开
// 会闪烁
fun TextView.resetAutoSize2(text: String) {
    val maxSize = TextViewCompat.getAutoSizeMaxTextSize(this)
    val minSize = TextViewCompat.getAutoSizeMinTextSize(this)
    if (maxSize <= minSize || minSize <= 0) return
    // 必须先关掉，才能修改TextSize
    TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE)
    setTextSize(TypedValue.COMPLEX_UNIT_PX, maxSize.toFloat())
    // 修改TextSize之后再开启
    post {
        try {
            setText(text)
            TextViewCompat
                .setAutoSizeTextTypeUniformWithConfiguration(
                    this,
                    minSize, maxSize, 1, TypedValue.COMPLEX_UNIT_PX
                )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun TextView.resetAutoSize(
    @Px originalSize: Float,
    @Px minSize: Float = 10.sp,
    @Px maxWidthP: Int = maxWidth,
    @Px maxHeightP: Int = -1,
) {
    setTextSize(
        TypedValue.COMPLEX_UNIT_PX, getAutoSize(
            paint,
            originalSize,
            text.toString(),
            maxWidthP,
            maxHeightP,
            minSize,
            maxLines
        )
    )
}


/**
 * 单词过长换行，则自动缩小到一行，获取对应的size
 * @param textPaint 需要设置字体
 * @param originalSize 原始size，单位px
 * @param maxWidth 容器最大宽度
 * @param minSize 最小size，低于则不再缩小处理
 * @param maxLine 最大行数
 */
fun getAutoSize(
    textPaint: TextPaint,
    @Px originalSize: Float,
    text: String,
    @Px maxWidth: Int,
    @Px maxHeight: Int = -1,
    @Px minSize: Float = 10.sp,
    maxLine: Int = 1000,
): Float {
    textPaint.isAntiAlias = true
    textPaint.textSize = originalSize
    var textSize = originalSize
    // 先测量全部文字
    var staticLayout = StaticLayout(
        text, textPaint, maxWidth, Layout.Alignment.ALIGN_CENTER,
        1.0f, 0.0f, false
    )
    while ((staticLayout.lineCount > maxLine || (maxHeight != -1 && staticLayout.height > maxHeight)) && textSize > minSize) {
        // 缩小字体 -1sp
        textSize -= 1.sp
        textPaint.textSize = textSize
        staticLayout = StaticLayout(
            text, textPaint, maxWidth, Layout.Alignment.ALIGN_CENTER,
            1.0f, 0.0f, false
        )
    }
    if (text.contains(" ")) {
        // 判断最长的单词是否超过，如果超过
        val split = text.split(" ")
        var maxStr = ""
        split.forEach {
            if (it.length > maxStr.length) {
                maxStr = it
            }
        }

        // 直接用textPaint.measure
        while (textPaint.measureText(maxStr) > maxWidth && textSize > minSize) {
            // 缩小字体 -1sp
            textSize -= 1.sp
            textPaint.textSize = textSize
        }
    }
    return textSize
}